package com.andorid.Download;

import java.io.File;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class mp3Activity extends Activity {
	public String PATH=Environment.getExternalStorageDirectory()+ "/download/";

	public String name;
	public ketnoi kn;
	public String duoi;
	public MediaPlayer mPlayer;
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mp3);
		name="wonder";
		duoi=".mp3";
		kn= new ketnoi();
		// tai file ve
		kn.DownloadFile("http://10.0.2.2/wonder.mp3",PATH,name,duoi);
		Button btn_Start = (Button)findViewById(R.id.btn_Start);
		Button btn_Stop =(Button)findViewById(R.id.btn_Stop);
		Button btn_Resume= (Button)findViewById(R.id.btn_Resume);
		Button btn_Pause =(Button)findViewById(R.id.btn_Pause);
		//lay duong dan file nhac

		//set cac su kien cho button
		btn_Start.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String nameFile=name+duoi;
				try {
					File myFile= new File(PATH, nameFile);	
					Uri myUri= Uri.fromFile(myFile);
					mPlayer= new MediaPlayer();
					mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
					mPlayer.setDataSource(getBaseContext(), myUri);
			
					mPlayer.prepare();
					mPlayer.start();
				} catch (Exception e) {
					// TODO: handle exception
				}
				}
		});
		btn_Pause.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mPlayer.pause();
			}
		});
		btn_Resume.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			mPlayer.reset();
			}
		});
		btn_Stop.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mPlayer.stop();
				
			}
		});
	}
}
